<!DOCTYPE html>
<html>
    <head>
        <title>Verify MediaKeySession.keyStatuses with multiple sessions</title>
        <script src="encrypted-media-utils.js"></script>
        <script src="../../resources/testharness.js"></script>
        <script src="../../resources/testharnessreport.js"></script>
    </head>
    <body>
        <script>
            async_test(function(test)
            {
                var mediaKeySession1;
                var mediaKeySession2;
                var initDataType;
                var initData;

                // Even though key ids are uint8, using printable values so that
                // they can be verified easily.
                var key1 = stringToUint8Array('123');
                var key2 = stringToUint8Array('4567890');
                var rawKey1 = new Uint8Array([0xeb, 0xdd, 0x62, 0xf1, 0x68, 0x14, 0xd2, 0x7b,
                                              0x68, 0xef, 0x12, 0x2a, 0xfc, 0xe4, 0xae, 0x3c]);
                var rawKey2 = new Uint8Array([0x3c, 0xae, 0xe4, 0xfc, 0x2a, 0x12, 0xef, 0x68,
                                              0x7b, 0xd2, 0x14, 0x68, 0xf1, 0x62, 0xdd, 0xeb]);

                function processMessage1(event)
                {
                    // This should only be called for session1.
                    assert_equals(event.target, mediaKeySession1);

                    // No keys added yet.
                    verifyKeyStatuses(mediaKeySession1.keyStatuses, []);

                    // Add key1 to session1.
                    var jwkSet = stringToUint8Array(createJWKSet(createJWK(key1, rawKey1)));
                    mediaKeySession1.update(jwkSet).catch(function(error) {
                        forceTestFailureFromPromise(test, error);
                    });
                }

                function processKeyStatusesChange1(event)
                {
                    // This should only be called for session1.
                    assert_equals(event.target, mediaKeySession1);

                    // Check that keyStatuses contains the expected key1 only.
                    dumpKeyStatuses(mediaKeySession1.keyStatuses);
                    verifyKeyStatuses(mediaKeySession1.keyStatuses,
                        [ { keyId: key1, status: 'usable' } ]);

                    // Now trigger a message event on session2.
                    mediaKeySession2.generateRequest(initDataType, initData).catch(function(error) {
                        forceTestFailureFromPromise(test, error);
                    });
                }

                function processMessage2(event)
                {
                    // This should only be called for session2.
                    assert_equals(event.target, mediaKeySession2);

                    // session2 has no keys added yet.
                    verifyKeyStatuses(mediaKeySession2.keyStatuses, []);

                    // session1 should still have 1 key.
                    verifyKeyStatuses(mediaKeySession1.keyStatuses,
                        [ { keyId: key1, status: 'usable' } ]);

                    // Add key2 to session2.
                    var jwkSet = stringToUint8Array(createJWKSet(createJWK(key2, rawKey2)));
                    mediaKeySession2.update(jwkSet).catch(function(error) {
                        forceTestFailureFromPromise(test, error);
                    });
                }

                function processKeyStatusesChange2(event)
                {
                    // This should only be called for session2.
                    assert_equals(event.target, mediaKeySession2);

                    // Check that keyStatuses contains the expected key2 only.
                    dumpKeyStatuses(mediaKeySession2.keyStatuses);
                    verifyKeyStatuses(mediaKeySession2.keyStatuses,
                        [ { keyId: key2, status: 'usable' } ]);

                    // session1 should still have 1 key.
                    verifyKeyStatuses(mediaKeySession1.keyStatuses,
                        [ { keyId: key1, status: 'usable' } ]);

                    test.done();
                }

                navigator.requestMediaKeySystemAccess('org.w3.clearkey', getSimpleConfiguration()).then(function(access) {
                    initDataType = access.getConfiguration().initDataTypes[0];
                    initData = getInitData(initDataType);
                    return access.createMediaKeys();
                }).then(function(mediaKeys) {
                    mediaKeySession1 = mediaKeys.createSession();
                    mediaKeySession2 = mediaKeys.createSession();

                    // There should be no keys defined on either session.
                    verifyKeyStatuses(mediaKeySession1.keyStatuses, []);
                    verifyKeyStatuses(mediaKeySession2.keyStatuses, []);

                    // Bind all the event handlers now.
                    waitForEventAndRunStep('message', mediaKeySession1, processMessage1, test);
                    waitForEventAndRunStep('message', mediaKeySession2, processMessage2, test);
                    waitForEventAndRunStep('keystatuseschange', mediaKeySession1, processKeyStatusesChange1, test);
                    waitForEventAndRunStep('keystatuseschange', mediaKeySession2, processKeyStatusesChange2, test);

                    // Generate a request on session1.
                    return mediaKeySession1.generateRequest(initDataType, initData);
                }).catch(function(error) {
                    forceTestFailureFromPromise(test, error);
                });
            }, 'Verify MediaKeySession.keyStatuses with multiple sessions.');
        </script>
    </body>
</html>
